import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', () => {
  let carts = ref([])
  function addcart(wgq, goods_id) {
    let username = localStorage.getItem('username')
    let pdcart = carts.value.find(item => item.goods.goods_id == goods_id)
    if (pdcart) {
      pdcart.number++
    }
    else {
      carts.value.push({
        username: username,
        id: Math.floor(Math.random() * 100000),
        goods: wgq,
        number: 1,
        checked: false,
      })
    }

  }
  return { carts, addcart }
},
  {
    persist: true,
  },
)
